/**
 * @file 基础工具
 * @author its-wild(https://gitee.com/its-wild)
 * @date 2021/07/13
 */
import JSEncrypt from './jsEncryptNew'

/**
 * 字符串 RSA 加密
 * @param publicKey 公钥
 * @param str 要加密的字符串
 * @returns 加密后的 base64 字符串 | false
 */
export const encryptStr = (publicKey: string, str: string): string | false => {
  const encrypt = new JSEncrypt()
  // 设置公钥
  encrypt.setPublicKey(publicKey)
  return encrypt.encryptLong(str)
}

/**
 * 字符串 RSA 解密
 * @param privateKey 私钥
 * @param str 要解密的 base64 字符串
 * @returns 解密后的字符串 | false
 */
export const decryptStr = (privateKey: string, str: string | false): string | false => {
  if (str === false) return false
  const decrypt = new JSEncrypt()
  // 设置私钥
  decrypt.setPrivateKey(privateKey)
  return decrypt.decryptLong(str)
}

export default {
  encryptStr,
  decryptStr
}
